import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录', hideInMenu: true }
  },
  {
    path: '/monitor',
    name: 'Monitor',
    component: () => import('@/views/MonitorCenter.vue'),
    meta: { title: '监控中心', icon: 'monitor', requiresAuth: true }
  },
  {
    path: '/streams',
    name: 'Streams',
    component: () => import('@/views/Streams.vue'),
    meta: { title: '直播流管理', icon: 'video-camera', requiresAuth: true }
  },
  {
    path: '/tasks',
    name: 'Tasks',
    component: () => import('@/views/Tasks.vue'),
    meta: { title: '任务管理', icon: 'list', requiresAuth: true }
  },
  {
    path: '/recording-clipping',
    name: 'RecordingClipping',
    component: () => import('@/views/RecordingClipping.vue'),
    meta: { title: '视频拆条', icon: 'scissors-cutting', requiresAuth: true }
  },
  {
    path: '/recording-clipping/:id',
    name: 'RecordingClippingPage',
    component: () => import('@/views/recording-clipping/RecordingClippingPage.vue'),
    meta: { title: '视频拆条', requiresAuth: true, hideInMenu: true }
  },
  {
    path: '/servers',
    name: 'Servers',
    component: () => import('@/views/Servers.vue'),
    meta: { title: '集群管理', icon: 'monitor', requiresAuth: true }
  },
  {
    path: '/users',
    name: 'UserManagement',
    component: () => import('@/views/UserManagement.vue'),
    meta: { title: '人员管理', icon: 'user', requiresAuth: true }
  },
  {
    path: '/storage',
    name: 'FileStorage',
    component: () => import('@/views/FileStorage.vue'),
    meta: { title: '文件存储', icon: 'folder', requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 如果访问登录页且已登录，跳转到监控页
  if (to.path === '/login' && token) {
    next('/monitor')
    return
  }
  
  // 如果需要认证且没有token，跳转到登录页
  if (to.meta.requiresAuth && !token) {
    next('/login')
    return
  }
  
  // 其他情况正常放行
  next()
})

export default router